package cn.stylefeng.guns.modular.fruits.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fruits.entity.FruitsBasicGoods;
import cn.stylefeng.guns.modular.fruits.mapper.FruitsBasicGoodsMapper;
import cn.stylefeng.guns.modular.fruits.model.params.FruitsBasicGoodsParam;
import cn.stylefeng.guns.modular.fruits.model.result.FruitsBasicGoodsResult;
import  cn.stylefeng.guns.modular.fruits.service.FruitsBasicGoodsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 商品管理 服务实现类
 */
@Service
public class FruitsBasicGoodsServiceImpl extends ServiceImpl<FruitsBasicGoodsMapper, FruitsBasicGoods> implements FruitsBasicGoodsService {

    @Override
    public void add(FruitsBasicGoodsParam param){
        FruitsBasicGoods entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FruitsBasicGoodsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FruitsBasicGoodsParam param){
        FruitsBasicGoods oldEntity = getOldEntity(param);
        FruitsBasicGoods newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FruitsBasicGoodsResult findBySpec(FruitsBasicGoodsParam param,Long operateId){
        return this.baseMapper.findBySpec(param, operateId);
    }

    @Override
    public List<FruitsBasicGoodsResult> findListBySpec(FruitsBasicGoodsParam param,Long operateId){
        return this.baseMapper.findListBySpec(param, operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(FruitsBasicGoodsParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition,operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FruitsBasicGoodsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FruitsBasicGoods getOldEntity(FruitsBasicGoodsParam param) {
        return this.getById(getKey(param));
    }

    private FruitsBasicGoods getEntity(FruitsBasicGoodsParam param) {
        FruitsBasicGoods entity = new FruitsBasicGoods();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
